﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmPermission : Office2007Form
    {
        #region Fields
        UserCtrl m_UserCtrl = new UserCtrl();
        UserGroupCtrl m_UserGroupCtrl = new UserGroupCtrl();
        PermissionCtrl m_PermissionCtrl = new PermissionCtrl(); 
        #endregion

        #region Contructor
        public frmPermission()
        {
            InitializeComponent();
        }
        #endregion
        
        #region Load
        private void frmPermission_Load(object sender, EventArgs e)
        {
            m_UserCtrl.LoadToComboBox(cmbUsers);
            m_UserGroupCtrl.LoadToComboBox(cmbUserGroup);
            m_UserCtrl.LoadToDataGridViewComboBoxColumn(colUserId);
            m_UserGroupCtrl.LoadToDataGridViewComboBoxColumn(colUserGroupId);
            m_PermissionCtrl.Show(dgvPermission, bindingNavigatorPermission, cmbUsers, cmbUserGroup);
            if (cmbUsers.SelectedValue.ToString().Equals("USR001"))
            {
                btnDongY.Enabled = false;
            }
            else
            {
                btnDongY.Enabled = true;
            }
        }
        #endregion

        #region Button Click
        private void btnDongY_Click(object sender, EventArgs e)
        {
            PermissionInfo info = new PermissionInfo(cmbUsers.SelectedValue.ToString(), cmbUserGroup.SelectedValue.ToString());
            m_PermissionCtrl.Update(info);
            m_PermissionCtrl.Show(dgvPermission, bindingNavigatorPermission, cmbUsers, cmbUserGroup);
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            m_PermissionCtrl.Show(dgvPermission, bindingNavigatorPermission, cmbUsers, cmbUserGroup);
        }
        #endregion
        
        #region BindingNavigator Item Event
        private void RefreshItem_Click(object sender, EventArgs e)
        {
            m_PermissionCtrl.Show(dgvPermission, bindingNavigatorPermission, cmbUsers, cmbUserGroup);
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region DataGridView Event
        private void dgvPermission_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.RowIndex == 0)
                {
                    btnDongY.Enabled = false;
                } 
                else
                {
                    btnDongY.Enabled = true;
                }
            }
            catch
            {}
        }

        private void dgvPermission_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {}
        #endregion
    }
}
